'use strict'
import { ajaxGetDictItems } from '@/api/api'

// utils is a library of generic helper functions non-specific to axios

var toString = Object.prototype.toString

/**
 * Determine if a value is an Array
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is an Array, otherwise false
 */
export function isArray (val) {
  return toString.call(val) === '[object Array]'
}


/**
 * Determine if a value is an Object
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is an Object, otherwise false
 */
export function isObject (val) {
  return val !== null && typeof val === 'object'
}

/**
 * Determine if a value is a Date
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a Date, otherwise false
 */
export function isDate (val) {
  return toString.call(val) === '[object Date]'
}

/**
 * Determine if a value is a URLSearchParams object
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a URLSearchParams object, otherwise false
 */
export function isURLSearchParams (val) {
  return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams
}


/**
 * Iterate over an Array or an Object invoking a function for each item.
 *
 * If `obj` is an Array callback will be called passing
 * the value, index, and complete array for each item.
 *
 * If 'obj' is an Object callback will be called passing
 * the value, key, and complete object for each property.
 *
 * @param {Object|Array} obj The object to iterate
 * @param {Function} fn The callback to invoke for each item
 */
export function forEach (obj, fn) {
  // Don't bother if no value provided
  if (obj === null || typeof obj === 'undefined') {
    return
  }

  // Force an array if not already something iterable
  if (typeof obj !== 'object') {
    /*eslint no-param-reassign:0*/
    obj = [obj]
  }

  if (isArray(obj)) {
    // Iterate over array values
    for (var i = 0, l = obj.length; i < l; i++) {
      fn.call(null, obj[i], i, obj)
    }
  } else {
    // Iterate over object keys
    for (var key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        fn.call(null, obj[key], key, obj)
      }
    }
  }
}

/**
 * 是否为boolean 值
 * @param val
 * @returns {boolean}
 */
export function isBoolean(val) {
  return typeof val === 'boolean'
}



/**
 * 获取字典数组
 * @param dictCode 字典Code
 * @return List<Map>
 */
// 系统是分开的，所以数据字典请求加上了sysUrl,配置不同，去不同的系统里查数据字典
export async function initDictOptions(dictCode,sysUrl) {
	console.log(new Date());
  if (!dictCode) {
    return '字典Code不能为空!'
  }
  //获取字典数组
  let res = await ajaxGetDictItems(dictCode,{},sysUrl)
  return res
}

export function datastime(type){
	let myDate = new Date();  //获取当前时间
	let yinian=myDate.getFullYear(); //年
	let yer=parseInt(myDate.getMonth())+1;//月份
	if(yer<=9){yer="0"+yer;}
	let riqi=myDate.getDate();//日
	if(riqi<=9){riqi="0"+riqi;}
	let time=myDate.getHours();//获取当前小时数(0-23)
	if(time<=9){time="0"+time;}
	let fen=myDate.getMinutes();     //获取当前分钟数(0-59)
	if(fen<=9){fen="0"+fen;}
	let miao=myDate.getSeconds();     //获取秒钟数
	if(miao<=9){miao="0"+miao;}
	if(type=='nyr'){return yinian+"年"+yer+"月"+riqi+"日"} // 2021年1月14日
	if(type=='yous'){
		return yinian+"-"+yer+"-"+riqi+" "+time+":"+fen+":"+miao //2021-01-14 16:47:36
	}
	if(type=='int'){return parseInt(yinian+""+yer+""+riqi+time+""+fen)}//202101141647
	return yinian+"年"+yer+"月"+riqi+"日"+" "+time+":"+fen+":"+miao //2021年1月14日 16:47:36
}



/**
 * 字典值替换文本通用方法
 * @param dictOptions  字典数组
 * @param text  字典值
 * @return String
 */
export function filterDictText(dictOptions, text) {
  // --update-begin----author:sunjianlei---date:20200323------for: 字典翻译 text 允许逗号分隔 ---
  if (text != null && Array.isArray(dictOptions)) {
    let result = []
    // 允许多个逗号分隔，允许传数组对象
    let splitText
    if (Array.isArray(text)) {
      splitText = text
    } else {
      splitText = text
        .toString()
        .trim()
        .split(',')
    }
    for (let txt of splitText) {
      let dictText = txt
      for (let dictItem of dictOptions) {
        if (txt.toString() === dictItem.value.toString()) {
          dictText = dictItem.text || dictItem.title || dictItem.label
          break
        }
      }
      result.push(dictText)
    }
    return result.join(',')
  }
  return text
  // --update-end----author:sunjianlei---date:20200323------for: 字典翻译 text 允许逗号分隔 ---
}

/**
 * @param {Object} n 数字金额转大写
 */

export function digitUppercase(n) {
	// 金额转大写
	const fraction = ['角', '分'];
	const digit = [
		'零', '壹', '贰', '叁', '肆',
		'伍', '陆', '柒', '捌', '玖'
	];
	const unit = [
		['元', '万', '亿'],
		['', '拾', '佰', '仟']
	];
	let head = n < 0 ? '负' : '';
	n = Math.abs(n);
	let s = '';
	for (let i = 0; i < fraction.length; i++) {
		s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
	}
	s = s || '整';
	n = Math.floor(n);
	for (let i = 0; i < unit[0].length && n > 0; i++) {
		let p = '';
		for (let j = 0; j < unit[1].length && n > 0; j++) {
			p = digit[n % 10] + unit[1][j] + p;
			n = Math.floor(n / 10);
		}
		s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
	}
	return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}
			

